package com.car.service;

import com.car.dao.CarDao;
import com.car.dao.PurchaseRecordDao;
import com.car.dao.UserCarDao;
import com.car.dao.UserDao;
import com.car.dao.impl.CarDaoImpl;
import com.car.dao.impl.PurchaseRecordDaoImpl;
import com.car.dao.impl.UserCarDaoImpl;
import com.car.dao.impl.UserDaoImpl;
import com.car.domain.Car;
import com.car.domain.PurchaseRecord;
import com.car.domain.User;
import com.car.domain.UserCar;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Scanner;

public class UserService {
    private static final UserDao userDao = new UserDaoImpl();
    private static final CarDao carDao = new CarDaoImpl();
    private static final UserCarDao userCarDao = new UserCarDaoImpl();
    private static final PurchaseRecordDao purchaseRecordDao = new PurchaseRecordDaoImpl();

    /**
     * 注册
     */
    public void register() throws Exception {
        Scanner scanner = new Scanner(System.in);
        User user = new User();
        System.out.println("用户注册");

        System.out.println("请输入用户名：");
        String name = scanner.next();
        user.setName(name);

        System.out.println("请输入密码：");
        String password = scanner.next();
        user.setPassword(password);

        System.out.println("请输入手机号：");
        String phone = scanner.next();
        user.setPhone(phone);

        System.out.println("请输入Email：");
        String email = scanner.next();
        user.setEmail(email);

        // 判断手机号是否已经存在数据库、
        // select * from t_user where phone = ?
        User dbUser = userDao.findByPhone(phone);
        if (dbUser != null) {
            System.out.println("注册失败，手机号已经存在。");
        } else {
            int i = userDao.add(user);
            if (i != 0) {
                System.out.println("注册成功，请登录。");
            } else {
                System.out.println("注册失败");
            }
        }
    }

    public User login(Scanner scanner) throws Exception {
        System.out.println("用户登录");
        System.out.println("请输入手机号：");
        String phone = scanner.next();
        System.out.println("请输入密码：");
        String password = scanner.next();
        return userDao.login(phone, password);
    }

    public void buyCar(User user, Scanner scanner) throws Exception {
        System.out.println("购买汽车");
        // 让用户输入汽车的id
        System.out.println("请输入要购买汽车的id：");
        int carId = scanner.nextInt();
        // 判断汽车是否存在并且未销售
        // select * from t_car where status = 0 and id = ?
        Car car = carDao.findByIdAndStatusEq0(carId);
        if (car == null){
            System.out.println("购买失败");
            return;
        }
        // 购买
        // 汽车状态修改
        car.setStatus(1);
        carDao.update(car);
        // 汽车中间表新增数据
        UserCar userCar = new UserCar();
        userCar.setCarId(carId);
        userCar.setUserId(user.getId());
        userCarDao.add(userCar);
        // 购买记录表新增记录
        PurchaseRecord pr = new PurchaseRecord();
        pr.setCarId(carId);
        pr.setUserId(user.getId());
        pr.setEmployeeId(car.getEmployeeId());
        pr.setPurchaseDatetime(LocalDateTime.now());
        purchaseRecordDao.add(pr);
        System.out.println("恭喜用户："+user.getName()+"购买"+car.getBrand() + car.getName()+"成功");

    }

    public List<Car> ownCars(User user) throws Exception{
        return userDao.findUserCarsById(user.getId());
    }

    public void updatePassword(User user,Scanner scanner) throws Exception {
        System.out.println("修改密码");
        System.out.println("请输入你要修改的密码：");
        String password = scanner.next();
        user.setPassword(password);
        userDao.update(user);
    }

    public void updateUserName(User user,Scanner scanner) throws Exception {
        System.out.println("修改用户名");
        System.out.println("请输入新的用户名：");
        String username = scanner.next();
        user.setName(username);
        userDao.update(user);
    }
}
